/*
 * Universidad del Valle de Guatemala
 * Algoritmos y Estructura de Datos
 * Sección: 10 
 * Carlos Alberto Solórzano Pineda (08832)
 * 12 de julio de 2011
 * Clase que muestra una ventana para organizar elementos gráficos
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.*;

@SuppressWarnings({ "unused", "serial" })
public class Window extends JFrame{

	// Objetos a utilizar
	Radio radio = new CarRadio();
	
	// Variables
	private boolean saving = false;
	
	public Window() {
		
		// Configuración de la ventana
		super();
		setTitle("CarRadio");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setSize(700, 200);
		setLocationRelativeTo(null);
		setVisible(true);
		
		initComponents();
	}
	
	/*
	 * Retorno: void
	 * Parámetros: no recibe
	 * Inicializa todos los componentes gráficos
	 * de la aplicación, así como los listeners
	 */
	private void initComponents(){
		
		// Objetos gráficos a utilizar
		JLabel welcome = new JLabel();
		final JLabel station = new JLabel();
		final JToggleButton on = new JToggleButton();
		final JToggleButton fa = new JToggleButton();
		final JToggleButton save = new JToggleButton();
		final JButton follow = new JButton();
		final JButton rewind = new JButton();
		final JButton[] memories = new JButton[12];
		final NumberFormat format = new DecimalFormat("0.#");
		GroupLayout layout = new GroupLayout(getContentPane());
		
		// Configuración de los objetos
		
		// Etiqueta welcome
		welcome.setText("Bienvenido a CarRadio...");
		
		// Etiqueta de estación
		station.setText("Usted está escuchando: ");
		
		// Botón on
		on.setText("On");
		on.setMnemonic('O');
		on.setToolTipText("Encender la radio");
		
		// Botón fa
		fa.setText("AM");
		fa.setMnemonic('A');
		fa.setToolTipText("Cambiar a AM");
		fa.setEnabled(false);
		
		// Botón save
		save.setText("Guardar estación");
		save.setMnemonic('G');
		save.setToolTipText("Guardar estación en memoria");
		save.setEnabled(false);
		
		// Botón follow
		follow.setText("Avanzar");
		follow.setMnemonic('V');
		follow.setToolTipText("Avanzar de estación");
		follow.setEnabled(false);
		
		// Botón rewind
		rewind.setText("Regresar");
		rewind.setMnemonic('R');
		rewind.setToolTipText("Retroceder de estación");
		rewind.setEnabled(false);
		
		// Configuración de listeners
		
		// Acciones para el botón de encendido
		on.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				radio.onoff();
				fa.setEnabled(radio.getOn());
				follow.setEnabled(radio.getOn());
				rewind.setEnabled(radio.getOn());
				save.setEnabled(radio.getOn());
				for (int i = 0; i < 12; i++){
					memories[i].setEnabled(radio.getOn());
				}
				if (!radio.getOn()){
					station.setText("Usted está escuchando: ");
				}else {
					station.setText("Usted está escuchando: " + format.format(radio.getEmisora()));
				}
			}
		});
		
		// Acciones para el botón de AM/FM
		fa.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				radio.change();
				if (!radio.getFM()){
					fa.setText("FM");
					fa.setToolTipText("Cambiar a FM");
					fa.setMnemonic('F');
				}else {
					fa.setText("AM");
					fa.setToolTipText("Cambiar a AM");
					fa.setMnemonic('A');
				}
				station.setText("Usted está escuchando: " + format.format(radio.getEmisora()));
			}
		});
		
		// Acciones para el botón follow
		follow.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				radio.avanzar();
				station.setText("Usted está escuchando: " + format.format(radio.getEmisora()));
			}
		});
		
		// Acciones para el botón rewind
		rewind.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				radio.retroceder();
				station.setText("Usted está escuchando: " + format.format(radio.getEmisora()));
			}
		});
		
		// Acciones para el botón save
		save.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event){
				saving = !saving;
			}
		});
		
		// Crear botones y sus ActionListeners
		for (int i = 0; i < 12; i++){
			final int j = i;
			memories[i] = new JButton();
			memories[i].setText("" + (i+1));
			memories[i].setEnabled(false);
			memories[i].addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event){
					if (saving) {
						radio.save(j);
						save.setSelected(false);
						saving = false;
					}else {
						radio.push(j);
						station.setText("Usted está escuchando: " + format.format(radio.getEmisora()));
					}
				}
			});
			
		}
		
		// Organizar elementos en GroupLayout
		getContentPane().setLayout(layout);
		
		layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(welcome)
	                    .addGroup(layout.createSequentialGroup()
	                    		.addComponent(memories[0])
	                    		.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[1])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[2])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[3])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[4])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[5])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[6])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[7])
	    	                	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	    	                	.addComponent(memories[8])
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                                .addComponent(on)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                                .addComponent(fa))
	                            .addGroup(layout.createSequentialGroup()
	                                .addComponent(memories[9])
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(memories[10])
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(memories[11]))))
	                    .addGroup(layout.createSequentialGroup()
	                        .addComponent(station)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 93, Short.MAX_VALUE)
	                        .addComponent(follow)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(rewind)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(save)))
	                .addContainerGap())
	        );
		
		// Configuración de los grupos de capas verticales
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(welcome)
	                    .addComponent(fa)
	                    .addComponent(on))
	                .addGap(30, 30, 30)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(station)
	                    .addComponent(follow)
	                    .addComponent(rewind)
	                    .addComponent(save))
	                .addGap(30, 30, 30)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                	.addComponent(memories[0])
	                	.addComponent(memories[1])
	                	.addComponent(memories[2])
	                	.addComponent(memories[3])
	                	.addComponent(memories[4])
	                	.addComponent(memories[5])
	                	.addComponent(memories[6])
	                	.addComponent(memories[7])
	                	.addComponent(memories[8])
	                	.addComponent(memories[9])
	                	.addComponent(memories[10])
	                	.addComponent(memories[11])
	                		)
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
		
		
	}

}
